home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0040.dms / q0040.adf / split / split.c < prev    next >
C/C++ Source or Header  |  1990-11-30  |  3KB  |  128 lines

  1. /* Split a large text file into a set of smaller files */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int  lump_size = 1000;
  7. char *pattern;
  8. char *in_file;
  9. char *out_file = "split.";
  10. char out_name[128];
  11. int  out_count = 0;
  12.  
  13. void
  14. doopts(argc,argv)
  15.     int argc;
  16.     char **argv;
  17.    {/* Get the command line options */
  18.     int i,got_out;
  19.  
  20.     got_out = 0;
  21.     for(i=1;i<argc;i++)
  22.        {if(argv[i][0] == '-')
  23.            {
  24.             switch(argv[i][1])
  25.                {
  26.                 case 'C': case 'c':
  27.                     out_count = atoi(&argv[i][2]);
  28.                     break;
  29.                 case 'L': case 'l':
  30.                     lump_size = atoi(&argv[i][2]);
  31.                     break;
  32.                 case 'p': case 'P':
  33.                     pattern = &argv[i][2];
  34.                     break;
  35.                 default:
  36.                     /* Tell the user the real set of options */
  37.                     goto out_options;
  38.                 }
  39.             }
  40.           else if(in_file==NULL)
  41.            {/* Data file name */
  42.             in_file = argv[i];
  43.             }
  44.           else if(!got_out)
  45.            {/* Data file name */
  46.             out_file = argv[i];
  47.             got_out = ~0;
  48.             }
  49.           else
  50.            {
  51. out_options:
  52.             printf("Options are\n\n");
  53.             printf("    -l<lump_length>\n");
  54.             printf("    -p<text_pattern>\n");
  55.             printf("    -c<initial_count>\n");
  56.             printf("    <source_file>\n");
  57.             printf("    [<out_file>]\n");
  58.             exit(20);
  59.             }
  60.         }
  61.     if(in_file==NULL)
  62.         goto out_options;
  63.     }
  64.  
  65. main(argc,argv)
  66.     int argc;
  67.     char **argv;
  68.    {/* Split a large file into smaller files */
  69.     FILE *in_fptr;
  70.     FILE *out_fptr;
  71.     int count;
  72.  
  73.     doopts(argc,argv);
  74.  
  75.     /* Open the input file */
  76.     in_fptr = fopen(in_file,"r");
  77.     if(in_fptr == NULL)
  78.        {printf("Cannot open file \"%s\"\n",in_file);
  79.         exit(20);
  80.         }
  81.  
  82.     count = 0;
  83.     while(!feof(in_fptr))
  84.        {/* So we must start another file */
  85.         sprintf(out_name,"%s%d",out_file,out_count++);
  86.         out_fptr = fopen(out_name,"w");
  87.         if(out_fptr == NULL)
  88.            {printf("Cannot open file \"%s\"\n",out_name);
  89.             exit(20);
  90.             }
  91.         if(pattern)
  92.            {/* Matching a pattern, take the simplest approach */
  93.             if(count)
  94.                 fputs(pattern,out_fptr);
  95.             count = 0;
  96.             while(!feof(in_fptr) && pattern[count]!='\0')
  97.                {/* Check out this char this will mess up if we try 
  98.                    to make it case insensitive */
  99.                 char next;
  100.                 next = fgetc(in_fptr);
  101.                 if(pattern[count] == next)
  102.                     ++count;
  103.                   else if (count)
  104.                    {int i;
  105.                     for(i=0;i<count;++i)
  106.                         fputc(pattern[i],out_fptr);
  107.                     count = 0;
  108.                     fputc(next,out_fptr);
  109.                     }
  110.                   else
  111.                    {fputc(next,out_fptr);
  112.                     }
  113.                 }
  114.             }
  115.           else
  116.            {/* Just counting out the bytes, should use fread() but 
  117.                I cannot be bothered */
  118.             count = 0;
  119.             while(!feof(in_fptr) && count<lump_size)
  120.                {fputc(fgetc(in_fptr),out_fptr);
  121.                 ++count;
  122.                 }
  123.             }
  124.         fclose(out_fptr);
  125.         }
  126.     fclose(in_fptr);
  127.     }
  128.